<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>后台管理系统</title>
    <link rel="stylesheet" type="text/css" href="/static/lib/admin-assets/layui/css/layui.css" />
    <link rel="stylesheet" type="text/css" href="/static/lib/admin-assets/css/login.css" />

    <script src="/static/lib/jquery3.6.0/jquery-3.6.0.min.js"></script>
    <style>
        #a-link{
            color:#b8b8b8;
            position: absolute;
            bottom:15px;
            right:50px;
        }
        #a-link:hover{
            color:#1f1f1f;
        }
        .m-login-btn{
            display: flex;
            justify-content: center;
        }
        .m-login-btn .layui-inline{
            width: 100%!important;
        }
    </style>
</head>

<body>
<div class="m-login-bg">
    <div class="m-login">
        <h3>注册新用户</h3>
        <div class="m-login-warp">
            <form class="layui-form">
                <input type="hidden" name="<?php echo csrf_token();?>" value="<?php echo csrf_hash();?>">
                <div class="layui-form-item">
                    <input type="text" name="username" required lay-verify="required" placeholder="新用户名" autocomplete="off" maxlength="20" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <input type="text" name="password" required lay-verify="required" placeholder="登录密码" autocomplete="off" maxlength="18" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <input type="text" name="email" required lay-verify="required" placeholder="邮箱" autocomplete="off" maxlength="30" class="layui-input">
                </div>
                <div class="layui-form-item m-login-btn">
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="login">点击注册</button>
                    </div>
                </div>
                <a href="/login" id="a-link">立即登录</a>
            </form>
        </div>
    </div>
    <p class="copyright" style="position:absolute;bottom:10px;color:white;text-align: center;width: 100%">Copyright 2015-2016 by XIAODU</p>
</div>
<script src="/static/lib/admin-assets/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function() {
        var form = layui.form(),
            layer = layui.layer;

        //监听提交
        form.on('submit(login)', function(data) {
            let {username,password,email} = data.field;
            // 表单验证
            if(!username || username.length<2){
                layer.msg("用户名的长度不能少于2个字符")
                return false;
            }
            if(!password || password.length<6){
                layer.msg("密码的长度不能少于6个字符")
                return false;
            }if(!email || email.length<9){
                layer.msg("邮箱的长度不能少于9个字符")
                return false;
            }
            // 提交表单
            $.ajax({
                url:"<?=route_to('doRegister')?>",
                method:'post',
                data:data.field,
                success:function(res){
                    res = JSON.parse(res)
                    layer.msg(res.msg);
                    if(res.code == 200){
                        setTimeout(()=>{
                            location.href="/login";
                        },1000)
                    }
                }
            })

            return false;
        });

    });
</script>
</body>

</html>